from pydantic import BaseModel, EmailStr, Field
from typing import Optional, List
from datetime import datetime


class UserBase(BaseModel):
    """用户基础模式"""
    username: str = Field(..., min_length=3, max_length=50, description="用户名")
    email: EmailStr = Field(..., description="邮箱地址")
    full_name: Optional[str] = Field(None, max_length=100, description="全名")
    is_active: bool = Field(True, description="是否激活")


class UserCreate(UserBase):
    """创建用户模式"""
    password: str = Field(..., min_length=6, max_length=50, description="密码")


class UserUpdate(BaseModel):
    """更新用户模式"""
    email: Optional[EmailStr] = None
    full_name: Optional[str] = None
    is_active: Optional[bool] = None
    password: Optional[str] = Field(None, min_length=6, max_length=50)


class UserInDB(UserBase):
    """数据库中的用户模式"""
    id: int
    is_superuser: bool
    created_at: datetime
    updated_at: Optional[datetime]
    last_login: Optional[datetime]

    class Config:
        from_attributes = True


class User(UserInDB):
    """返回给客户端的用户模式"""
    roles: List["Role"] = []


class RoleBase(BaseModel):
    """角色基础模式"""
    name: str = Field(..., max_length=50, description="角色名称")
    description: Optional[str] = Field(None, max_length=200, description="角色描述")
    is_active: bool = Field(True, description="是否激活")


class RoleCreate(RoleBase):
    """创建角色模式"""
    permission_ids: List[int] = Field([], description="权限ID列表")


class RoleUpdate(BaseModel):
    """更新角色模式"""
    name: Optional[str] = None
    description: Optional[str] = None
    is_active: Optional[bool] = None
    permission_ids: Optional[List[int]] = None


class Role(RoleBase):
    """角色模式"""
    id: int
    created_at: datetime
    permissions: List["Permission"] = []

    class Config:
        from_attributes = True


class PermissionBase(BaseModel):
    """权限基础模式"""
    name: str = Field(..., max_length=50, description="权限名称")
    description: Optional[str] = Field(None, max_length=200, description="权限描述")
    resource: str = Field(..., max_length=50, description="资源类型")
    action: str = Field(..., max_length=50, description="操作类型")


class PermissionCreate(PermissionBase):
    """创建权限模式"""
    pass


class Permission(PermissionBase):
    """权限模式"""
    id: int
    created_at: datetime

    class Config:
        from_attributes = True


class Token(BaseModel):
    """访问令牌模式"""
    access_token: str
    token_type: str = "bearer"
    expires_in: int
    user: User


class TokenData(BaseModel):
    """令牌数据模式"""
    user_id: Optional[int] = None
    username: Optional[str] = None


class LoginRequest(BaseModel):
    """登录请求模式"""
    username: str = Field(..., description="用户名或邮箱")
    password: str = Field(..., description="密码")


class ChangePasswordRequest(BaseModel):
    """修改密码请求模式"""
    old_password: str = Field(..., description="旧密码")
    new_password: str = Field(..., min_length=6, max_length=50, description="新密码")


class ResetPasswordRequest(BaseModel):
    """重置密码请求模式"""
    email: EmailStr = Field(..., description="邮箱地址")


class UserProfile(BaseModel):
    """用户资料模式"""
    id: int
    username: str
    email: str
    full_name: Optional[str]
    is_active: bool
    is_superuser: bool
    created_at: datetime
    last_login: Optional[datetime]
    roles: List[Role]

    class Config:
        from_attributes = True


# 更新前向引用
User.model_rebuild()
Role.model_rebuild() 